package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.core.stock.StockUtil;
import mobi.hami.huapin.dao.UserOrderMapper;
import mobi.hami.huapin.dto.SKUPresentDTO;
import mobi.hami.huapin.dto.UserOrderDTO;
import mobi.hami.huapin.model.HuaPin;
import mobi.hami.huapin.model.UserOrder;
import mobi.hami.huapin.model.UserPresentSku;
import mobi.hami.huapin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by 小峰 on 2018/9/1.
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {

    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private HuaPinService huaPinService;
    @Autowired
    private UserWechatService userWechatService;
    @Autowired
    private UserNoteService userNoteService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private UserPresentSkuService userPresentSkuService;
    @Autowired
    private HauPinTaskService hauPinTaskService;
    @Autowired
    private UserOrderDetailService userOrderDetailService;


    @Override
    public UserOrderDTO getOneById(Integer id) {
        return userOrderMapper.getOneById(id);
    }

    @Override
    public List<UserOrderDTO> getListByHuapnId(Integer id) {
        return userOrderMapper.getListByHuapinId(id);
    }

    @Override
    public List<UserOrderDTO> getListByUserId(Integer id) {
        return userOrderMapper.getListByUserId(id);
    }

    /**
     * 获取昨天16点到当前时间未付款的订单
     *
     * @return
     */
    public List<UserOrderDTO> getYesterdayUnPayList() {
        return userOrderMapper.getYesterdayUnPayList();
    }


    @Override
    public int saveModel(UserOrder userOrder) {
        userOrderMapper.insertSelective(userOrder);
        return userOrder.getId();
    }

    /**
     * 更新订单状态处理
     *
     * @param orderCode
     * @param money
     * @return
     */
    @Override
    public int updateOrderPayStatusByOrderId(String orderCode, BigDecimal money) {
        Integer orderStatus = 2;

        UserOrder userOrder = userOrderMapper.getOrderByCode(orderCode);
        HuaPin huaPin = huaPinService.selectOnbyId(userOrder.getHuapin_id());
        if (huaPin.getType().equals(2)) {
            if (huaPin.getUser_id().equals(userOrder.getUser_id())) {
                userWechatService.updateLaunchCollageByUserId(userOrder.getUser_id());
            } else {
                userWechatService.updatePartackCollageByUserId(userOrder.getUser_id());
            }
        } else if (huaPin.getType().equals(1)) { //个人拼团处理
            userWechatService.updateselfCollageByUserId(userOrder.getUser_id());
            huaPin.setStatus(2);
            huaPinService.update(huaPin);
            orderStatus = 4;
            List<SKUPresentDTO> skuPreSentList = subjectService.getSKUPreSentListBySubjectId(10);
            List<UserPresentSku> presentCountList = userPresentSkuService.getUserPresentCount(userOrder.getUser_id());
            if (presentCountList == null) {
                presentCountList = new ArrayList<>();
            }
            SKUPresentDTO skuPresentDTO = hauPinTaskService.calculatePresentSKU(skuPreSentList, presentCountList, null);
            userOrderDetailService.instanceUserOrderDetail(skuPresentDTO, userOrder.getId(),userOrder.getOrder_code());
            userPresentSkuService.saveUserPresentSkuByModel(userOrder.getUser_id(), huaPin.getId(), userOrder.getId(), skuPresentDTO.getId());
            new StockUtil().actualRepertoryByOrderCode(userOrder.getOrder_code(),"wms/stock/actualSubRepertory.do");
        }
        return userOrderMapper.updateOrderPayStatusByOrderId(orderCode, money, orderStatus);
    }

    @Override
    public List<UserOrder> taskCallInvokeCollage() {
        return userOrderMapper.taskCallInvokeCollage();
    }

    @Override
    public int updateOrderByPrimaryKeySelective(UserOrder userOrder) {
        return userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }

    @Override
    public int updateSendStatus(Map<String, Object> mapStatus) {
        Integer orderStatus = 0;
        if (mapStatus.get("order_userid").equals(mapStatus.get("current_userid"))) {
            orderStatus = 7;
        } else {
            orderStatus = 6;
        }
        Integer orderId = Integer.parseInt(String.valueOf(mapStatus.get("order_id")));
        userOrderMapper.updateSendStatus(orderId, orderStatus);

        UserOrder userOrder = userOrderMapper.selectByPrimaryKey(orderId);
        HuaPin huaPin = huaPinService.selectOnbyId(userOrder.getHuapin_id());
        userNoteService.addOrderUserNoteInfo(orderId, huaPin.getAddress_id(), userOrder.getUser_id());
        return orderStatus;
    }

    @Override
    public UserOrder findOneByModel(UserOrder userOrder) {
        return userOrderMapper.selectOne(userOrder);
    }


    /**
     * 根据地址和用户Id　获取购买次数
     *
     * @param userId
     * @param addressId
     * @return
     */
    @Override
    public int findCountByuserIdAndAddressid(Integer userId, Integer addressId) {
        return userOrderMapper.findCountByuserIdAndAddressid(userId, addressId);
    }


    /**
     * 更新订单的收货状态
     * 当团长已经收货后,在24小时后没的更改收货状态,程序自动更新
     *
     * @return
     */
    @Override
    public void taskUpdateSendStatus() {
        userOrderMapper.taskUpdateSendStatus();
    }
}
